package com.example.exam.ZhangFengHua;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.exam.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static androidx.constraintlayout.widget.Constraints.TAG;

public class SharedPreferences extends Fragment {
    @BindView(R.id.et_shared_srk)
    EditText etSharedSrk;
    @BindView(R.id.bu_shared_cc)
    Button buSharedCc;
    @BindView(R.id.bu_shared_cc1)
    Button buSharedCc1;
    @BindView(R.id.et_shared_srk1)
    EditText etSharedSrk1;

    public SharedPreferences() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.sharedpreferences, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    //存储
    @OnClick(R.id.bu_shared_cc)
    public void onViewClicked() {
        android.content.SharedPreferences.Editor editor = getContext().getSharedPreferences("zhangfenghua", Context.MODE_PRIVATE).edit();
        editor.putString("name",etSharedSrk.getText().toString().trim());
        editor.apply();
        etSharedSrk.setText("");
        Toast.makeText(getContext(), "储存成功", Toast.LENGTH_SHORT).show();
    }
    //读取
    @OnClick(R.id.bu_shared_cc1)
    public void onViewClicked1() {
        android.content.SharedPreferences pref = getContext().getSharedPreferences("zhangfenghua", Context.MODE_PRIVATE);
        etSharedSrk1.setText( pref.getString("name",""));
        Toast.makeText(getContext(), "获取成功", Toast.LENGTH_SHORT).show();
    }
}
